<template>
  <div class="common-header">
    <div class="neirong">
      <div class="zuozu" v-show="isLogin"  @mouseenter="showBox" 
        @mouseleave="hideBoxIfNotClicked" 
        @click="toggleBoxVisibility">
        <span class="colorzi">你好，</span>
        <span style="margin-right: 8px;" class="colorzi">{{user.name||'梯语云用户'}}</span>
        <i class="el-icon-arrow-down colorzi"></i>
        
      </div>
      <div class="hezi"  :style="{ display: isBoxVisible ? 'flex' : 'none' }">
          <div class="zuotou">
            <img :src="user.avatar?user.avatar:'https://picsum.photos/60/60'" alt="">
          </div>
          <div class="youhe">
            <div class="name">{{user.name||'梯语云用户'}}</div>
            <div class="row2">
              <!-- <span @click="gogeren">个人中心</span>
              <el-divider direction="vertical"></el-divider> -->
              <span @click="logout">退出</span>
            </div>
          </div>
        </div>
      <div class="zuozu" v-show="!isLogin" @click="gologin" >
        <span >你好，</span>
        <span class="colorzi">请登录</span>
      </div>
      <div class="youzu">
         <el-menu :default-active="activeIndex" router class="el-menu-demo" mode="horizontal" @select="handleSelect">
          <el-menu-item index="1" route="/pagesContent">首页</el-menu-item>
          <el-menu-item index="2" route="/personalCenter/myorder">个人中心</el-menu-item>
        </el-menu>
        <!-- <el-popover
        placement="bottom"
        width="160"
         trigger="hover"
        >
        <p>客服电话</p>
        <span class="itemtab" slot="reference">
          联系客服
        </span>
      </el-popover>
        
       
        <span @click="goaboutus">
          关于我们
        </span> -->
      </div>
    </div>
  </div>
</template>

<script>
import {mapState, mapActions } from 'vuex';
import { removetoken,gettoken } from '@/utils/auth'

export default {
  
  data() {
    return {
      activeIndex:'1',
      isBoxVisible:false,
      username:'',
    }
  },
  computed: {
    ...mapState(['user']),
     isLogin(){
      //  console.log(this.$store.state.user.token)
      return this.$store.state.user.name?true:false
      
    }
  },
  watch: {
  
    isLogin(val) {
      if(val){
        this.showlogin=true
      }else{
        this.showlogin=false
      }
      
    }
  },
  mounted() {
    var huoqu = window.location.href
    if(huoqu.indexOf('personalCenter/myorder')!=-1){
      this.activeIndex='2'
    }else if(huoqu.indexOf('pagesContent')!=-1){
      this.activeIndex='1'
    }


    this.init()
    document.addEventListener('click', this.handleOutsideClick);
  },
  beforeDestroy() {
    document.removeEventListener('click', this.handleOutsideClick);
  },
  methods: {
    handleSelect(key, keyPath) {
      // console.log(key, keyPath);
      if(key=='2'){
        this.changeCurrentmenu('1')
      }

    },
    ...mapActions(['openLoginDialog','setname','changeCurrentmenu']),
    gologin(){
      
      this.openLoginDialog()
    },
    goaboutus(){
      this.$router.push('/aboutus')
    },
    gogeren(){
    this.isBoxVisible=false
      this.$router.push('/personalCenter')
    },
    logout(){
      this.isBoxVisible=false
     this.$confirm('确定退出梯语云保险吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.showlogin=false
          removetoken()
         this.setname('')


          this.$router.push({ path: '/login' })
         
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });          
      });
     
     
    },
    showBox() {
      if (!this.isBoxClicked) {
        this.isBoxVisible = true;
      }
    },
    hideBoxIfNotClicked() {
      if (!this.isBoxClicked) {
        this.isBoxVisible = false;
      }
    },
    toggleBoxVisibility() {
      this.isBoxClicked = !this.isBoxClicked;
      this.isBoxVisible = this.isBoxClicked;
    },
    handleOutsideClick(event) {
      const hoverElement = this.$el.querySelector('.zuozu');
      const hoverBox = this.$el.querySelector('.hezi');
      if (
        this.isBoxClicked &&
        !hoverElement.contains(event.target) &&
        !hoverBox.contains(event.target)
      ) {
        this.isBoxVisible = false;
        this.isBoxClicked = false;
      }
    },
    async init(){
      var token =gettoken()
      // console.log(token)
      // console.log(this.isLogin)
      if(token){
        this.showlogin=true
      }else{
        this.showlogin=false
      }
      

      // const res =await getcurrentinfo({
      //   appId:'wx4df62dce16841dee'
      // })
      // this.appInfo=res.data
      // localStorage.setItem('appInfo', JSON.stringify(res.data));
    }
  }
}
</script>

<style lang="scss" scoped>
.common-header {
 
  width: 100%;
  
  height: 68px;
  background: #FFFFFF;
  border-radius: 0px 0px 0px 0px;
 
  .neirong{
   position: relative;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
     display: flex;
    align-items: center;
    justify-content: space-between;
    height: 68px;
    background-image: url('../../assets/images/searchbox_bg.png');
    background-repeat: no-repeat;
    background-size: cover;
  }
  .zuozu {
    display: flex;
    align-items: center;
    font-size: 16px;
    color: #333333;
    
    .colorzi {
      color: #0672F9;
        cursor: pointer;
    }
  }
  .hezi{
    position: absolute;
    top: 48px;
    left: 0;
    width: 236px;
    height: 92px;
    background: #FFFFFF;
    box-shadow: 0px 0px 12px 1px rgba(0,0,0,0.1);
    border-radius: 8px 8px 8px 8px;
    align-items: center;
    justify-content: center;
    z-index: 99999;
    .zuotou{
      width: 60px;
      height: 60px;
      border-radius: 60px;
      overflow: hidden;
      img{
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    .youhe{
      margin-left: 10px;
      .name{
        font-weight: 400;
        font-size: 20px;
        color: #000000;
        margin-bottom: 14px;
      }
      .row2{
        font-weight: 400;
        font-size: 16px;
        color: #4F4F4F;
        text-align: left;
        cursor: pointer;
      }
    }
  }
  .youzu {
    flex: 1;
    margin-left: 54px;
    display: flex;
    align-items: center;
    // justify-content: flex-end;
    font-size: 16px;
    color: #333333;
    ::v-deep .el-menu{
      background-color: transparent;
      border-bottom: none;
    }
    ::v-deep .el-menu-item{
      font-size: 18px;
    }
    ::v-deep .el-menu--horizontal > .el-menu-item:not(.is-disabled):hover, ::v-deep .el-menu--horizontal > .el-menu-item:not(.is-disabled):focus{
      background-color: transparent;
    }
    span {
      margin-left: 10px;
      color: #4F4F4F;
      cursor: pointer;
    }
  }

}
</style>
